https://leetcode.cn/problems/implement-queue-using-stacks/


typedef int STDatatype;
typedef struct Stack
{
    STDatatype* a;
    int top;
    int capacity;
}Stack;

void StackInit(Stack* ps)
{
    assert(ps);
    ps->a = NULL;
    ps->top = ps->capacity = 0;
}
void StackPush(Stack* ps, STDatatype x)
{
    assert(ps);
    if (ps->top == ps->capacity)
    {
        ps->capacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
        STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * sizeof(STDatatype));
        assert(tmp);
        ps->a = tmp;

    }
    ps->a[ps->top] = x;
    ps->top++;


}
void StackPop(Stack* ps)
{
    assert(ps);
    assert(ps->top);
    ps->top--;

}
STDatatype StackTop(Stack* ps)
{
    assert(ps);
    assert(ps->top);
    return ps->a[ps->top - 1];
}
int StackSize(Stack* ps)
{
    assert(ps);
    return ps->top;
}
void StackDestroy(Stack* ps)
{
    assert(ps);
    free(ps->a);
    ps->top = ps->capacity = 0;
}


bool StackEmpty(Stack* ps)
{
    assert(ps);
    if (ps->top == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

typedef struct {
    Stack s1;
    Stack s2;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* pst = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&pst->s1);
    StackInit(&pst->s2);
    return pst;
}

void myQueuePush(MyQueue* obj, int x) {
    if (!StackEmpty(&obj->s1))
    {
        StackPush(&obj->s1, x);
    }
    else {
        StackPush(&obj->s2, x);

    }

}

int myQueuePop(MyQueue* obj) {
    Stack* empty = &obj->s1;
    Stack* nonEmpty = &obj->s2;
    if (!StackEmpty(&obj->s1))
    {
        nonEmpty = &obj->s1;
        empty = &obj->s2;
    }

    while (StackSize(nonEmpty) > 1)
    {
        int back = StackTop(nonEmpty);
        StackPush(empty, back);
        StackPop(nonEmpty);
    }
    int tmp = StackTop(nonEmpty);
    StackPop(nonEmpty);

    while (StackSize(empty))
    {
        int back = StackTop(empty);
        StackPush(nonEmpty, back);
        StackPop(empty);
    }
    return tmp;



}

int myQueuePeek(MyQueue* obj) {
    Stack* empty = &obj->s1;
    Stack* nonEmpty = &obj->s2;
    if (!StackEmpty(&obj->s1))
    {
        nonEmpty = &obj->s1;
        empty = &obj->s2;
    }

    while (StackSize(nonEmpty) > 1)
    {
        int back = StackTop(nonEmpty);
        StackPush(empty, back);
        StackPop(nonEmpty);
    }
    int tmp = StackTop(nonEmpty);
    StackPop(nonEmpty);
    StackPush(empty, tmp);

    while (StackSize(empty))
    {
        int back = StackTop(empty);
        StackPush(nonEmpty, back);
        StackPop(empty);
    }
    return tmp;

}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->s1) && StackEmpty(&obj->s2);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->s1);
    StackDestroy(&obj->s2);
    free(obj);


}